<template>
  <div>
    <el-row type="flex" justify="end">
      <!-- 选择年 -->
      <el-select v-model="currentYear" size="small" @change="changeDate">
        <el-option v-for="item in yearList" :key="item" :label="item" :value="item" />
      </el-select>
      <!-- 选择月 -->
      <el-select v-model="currentMonth" size="small" @change="changeDate">
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>
    </el-row>
    <el-calendar v-model="value">
      <template #dateCell="{date, data}">
        <!-- {{ date }}日期对象 -->
        <!-- {{ data }} {"day": "2022-02-28" } -->
        <!-- <span v-if="isWeek(date)">休</span> -->
        <div class="date-content">
          <span class="text"> {{ getDay(data.day) }}</span>
          <!-- <span v-if="isWeek(date)">休</span> -->
          <span v-if="isWeek2(data.day)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: new Date(),
      currentMonth: '',
      currentYear: '',
      // 模拟的排班表发送请求回来的数据
      list: ['2022-03-08', '2022-03-11', '2022-03-15', '2022-03-20', '2022-03-22', '2022-03-23', '2022-03-28']
    }
  },
  computed: {
    yearList() {
      return Array.from(new Array(11), (item, index) => this.currentYear - 5 + index)
    }
  },
  watch: {
    value(newval) {
      // console.log('你变了', newval)
      // 监听value的变化 值是最新的日期对象, 基于这个最新的日期对象 得到最新的年和月
      this.currentMonth = newval.getMonth() + 1
      this.currentYear = newval.getFullYear()
    }
  },
  created() {
    this.currentMonth = this.value.getMonth() + 1
    this.currentYear = this.value.getFullYear()
  },
  methods: {
    // 1. 为两个下拉框绑定change事件 获取到了最新的年和月
    // 2. 基于最新的年月生成新的日期对象 交给value
    changeDate() {
      // console.log(this.currentYear, this.currentMonth)
      this.value = new Date(`${this.currentYear}-${this.currentMonth}`)
    },
    getDay(day) {
      return day.split('-')[2].startsWith('0') ? day.split('-')[2][1] : day.split('-')[2]
    },
    // 周末休
    isWeek(date) {
      if (date.getDay() === 6 || date.getDay() === 0) {
        return true
      } else {
        return false
      }
    },
    // 模拟上班表
    isWeek2(day) {
      return this.list.includes(day)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-calendar__header{
  display: none;
}
.select-box {
  display: flex;
  justify-content: flex-end;
}

::v-deep .el-calendar-day {
  height:  auto;
 }
::v-deep .el-calendar-table__row td::v-deep .el-calendar-table tr td:first-child, ::v-deep .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
::v-deep .el-calendar__header {
   display: none
 }
</style>
